package com.ayf.payment.game.api.service.impl;

import com.ayf.payment.game.api.dto.GroupsDTO;
import com.ayf.payment.game.api.dto.criteria.GroupsSelectCriteria;
import com.ayf.payment.game.api.entity.GroupAndPartition;
import com.ayf.payment.game.api.entity.Groups;
import com.ayf.payment.game.api.entity.GroupsId;
import com.ayf.payment.game.api.entity.Partition;
import com.ayf.payment.game.api.mapper.GroupAndPartitionMapper;
import com.ayf.payment.game.api.mapper.GroupsIdMapper;
import com.ayf.payment.game.api.mapper.GroupsMapper;
import com.ayf.payment.game.api.mapper.PartitionMapper;
import com.ayf.payment.game.api.service.GroupAndPartitionService;
import com.ayf.payment.game.api.service.GroupsService;
import com.ayf.payment.game.api.type.Status;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
public class GroupsServiceImpl implements GroupsService {

    @Resource
    private GroupsMapper groupsMapper;

    @Resource
    private PartitionMapper partitionMapper;
    @Resource
    private GroupAndPartitionMapper groupAndPartitionMapper;

    @Resource
    GroupAndPartitionService groupAndPartitionService;

    @Resource
    GroupsIdMapper groupsIdMapper;

    @Override
    public List<GroupsDTO> selectByDTO(Integer merchantId) {
        return groupsMapper.selectByDTO(merchantId);
    }

    @Override
    public int save(Groups t) {
        if(StringUtils.isEmpty(t.getGroupName())){
            throw new IllegalArgumentException("参数不能为空");
        }
        GroupsId groupsId = new GroupsId();
        groupsIdMapper.insert(groupsId);
        t.setId(String.valueOf(groupsId.getId()));
        t.setCreateTime(new Date());
        t.setLastUpdateTime(new Date());
        t.setGroupStatus(Status.ENABLE);
        return groupsMapper.insert(t);
    }

    @Override
    public void update(Groups t) {
        if(StringUtils.isEmpty(t.getGroupName())){
            throw new IllegalArgumentException("参数不能为空");
        }
        groupsMapper.updateByPrimaryKeySelective(t);
    }

    @Override
    public void delete(GroupsDTO groupsDTO) {
        if (CollectionUtils.isEmpty(groupsDTO.getGroupsIds())){
            if (StringUtils.isNotEmpty(groupsDTO.getId())){
                List<String> groupsIds = new ArrayList<>();
                groupsIds.add(groupsDTO.getId());
                groupsDTO.setGroupsIds(groupsIds);
            }
        }
        if (!CollectionUtils.isEmpty(groupsDTO.getGroupsIds())){
            groupsDTO.getGroupsIds().forEach(groupIdItem->{
                Groups groups = new Groups();
                groups.setId(groupIdItem);
                groups.setGroupStatus(Status.PROHIBIT);
                groupsMapper.updateByPrimaryKeySelective(groups);
            });
        }
    }

    @Override
    public Groups find(Integer merchantId,String id) {
        if(StringUtils.isEmpty(id)){
            throw new IllegalArgumentException("参数不能为空");
        }
        return groupsMapper.selectById(merchantId,id);
    }

    @Override
    public GroupsDTO findDTO(Integer merchantId, String id) {
        if(StringUtils.isEmpty(id)){
            throw new IllegalArgumentException("参数不能为空");
        }
        return groupsMapper.selectByIdDTO(merchantId,id);
    }

    @Override
    public List<GroupsDTO> selectGroupInfo(GroupsSelectCriteria groupsSelectCriteria) {
        return groupsMapper.selectGroupInfo(groupsSelectCriteria);
    }

    @Override
    public String addTuiGroup(Groups groups) {
        save(groups);
        Partition partition=new Partition();
        partition.setMerchantId(groups.getMerchantId());
        partition.setPartitionStatus(Status.ENABLE);
        List<Partition> select = partitionMapper.select(partition);
        if (!CollectionUtils.isEmpty(select)) {
            List<GroupAndPartition> list = new ArrayList<>();
            for (Partition partition1 : select) {
                GroupAndPartition gsp = new GroupAndPartition();
                gsp.setPartitionId(partition1.getId());
                gsp.setGroupsId(groups.getId());
                gsp.setGroupName(groups.getGroupName());
                list.add(gsp);
            }
            groupAndPartitionMapper.insertBatch(list);
        }
        return groups.getId();
    }

    @Override
    public List<GroupsDTO> selectList(GroupsDTO groupsDTO) {
        return groupsMapper.selectList(groupsDTO);
    }
}


